// PropTab.cpp : Implementation of CPropTab
#include "stdafx.h"
#include "PropTab.h"

STDMETHODIMP CPropTab::GetControlInfo(LPCONTROLINFO lpCI)
{
	if(m_isPropTabOpened)
	{	
		m_isLoading = true;
		VARIANT_BOOL enabled, alwayson, useregex, checksubj;
		tstring mask;
		m_settings.getSettings(enabled, alwayson, useregex, checksubj, mask);
		CWindow::SendDlgItemMessage(IDC_CHECK_ENABLED, BM_SETCHECK,enabled );
		CWindow::SendDlgItemMessage(IDC_CHECK_ENCITALW, BM_SETCHECK,alwayson );
		CWindow::SendDlgItemMessage(IDC_CHECK_USEREGEX, BM_SETCHECK,useregex );
		CWindow::SendDlgItemMessage(IDC_CHECK_CHECKSUBJ, BM_SETCHECK,checksubj );
		CWindow::SetDlgItemText(IDC_EDIT_MASK, mask.c_str());
		m_isLoading = false;
	}
    return S_OK;
}

STDMETHODIMP CPropTab::GetPageInfo(BSTR *HelpFile,LONG *HelpContext)
{
	if (HelpFile == NULL)
		return E_POINTER;
	if (HelpContext == NULL)
		return E_POINTER;
	return E_NOTIMPL;	
}

STDMETHODIMP CPropTab::SetClientSite(IOleClientSite *pClientSite)
{
	HRESULT result;	
	// call default ATL implementation
	result = IOleObjectImpl<CPropTab>::SetClientSite(pClientSite);
	if (result != S_OK) return result;
	// pClientSite may be NULL when container has being destructed
	if (pClientSite != NULL) {
		CComQIPtr<Outlook::PropertyPageSite> pPropertyPageSite(pClientSite);
		result = pPropertyPageSite.CopyTo(&m_pPropPageSite);
	}
	if(!m_isPropTabOpened)
		m_settings.load();
	m_isPropTabOpened = !m_isPropTabOpened;		
	return result;
}

STDMETHODIMP CPropTab::get_Dirty(VARIANT_BOOL *Dirty)
{
	*Dirty = isDirty();
	return S_OK;
}

STDMETHODIMP CPropTab::Apply()
{	
	m_settings.apply();
	return S_OK;
}

LRESULT CPropTab::OnBnClickedCheckEnabled(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if(m_isLoading)
		return S_OK;
	VARIANT_BOOL checked = CWindow::IsDlgButtonChecked(wID);
	m_settings.enabled = checked;
	m_pPropPageSite->OnStatusChange();
	return S_OK;
}

LRESULT CPropTab::OnBnClickedCheckEncitalw(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandle)
{
	if(m_isLoading)
		return S_OK;
	VARIANT_BOOL checked = CWindow::IsDlgButtonChecked(wID);
	m_settings.alwayson = checked;
	m_pPropPageSite->OnStatusChange();
	return S_OK;
}

LRESULT CPropTab::OnBnClickedCheckUseregex(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if(m_isLoading)
		return S_OK;
	VARIANT_BOOL checked = CWindow::IsDlgButtonChecked(wID);
	m_settings.useregex = checked;
	m_pPropPageSite->OnStatusChange();
	return S_OK;
}

LRESULT CPropTab::OnBnClickedCheckChecksubj(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if(m_isLoading)
		return S_OK;
	VARIANT_BOOL checked = CWindow::IsDlgButtonChecked(wID);
	m_settings.checksubj = checked;
	m_pPropPageSite->OnStatusChange();
	return S_OK;
}

LRESULT CPropTab::OnEnChangeEditMask(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{  
	if(m_isLoading)
		return S_OK;
	const size_t maskLength = 100;
	TCHAR txt[maskLength] = {};
	CWindow::GetDlgItemText(wID, txt, maskLength);
	m_settings.mask = txt;
	m_pPropPageSite->OnStatusChange();
	return S_OK;
}
